<!--
 * @Descripttion: 数据字典
 * @version: 1.0
 * @Author: 肖亚龙
 * @Date: 2022年1月5日
 * @LastEditors:
 * @LastEditTime:
-->

<template>
  <div class="sc-select">
    <div v-if="initloading" class="sc-select-loading">
      <el-icon class="is-loading"><el-icon-loading /></el-icon>
    </div>
    <el-cascader
      v-bind="$attrs"
      :options="options"
      :loading="loading"
      :props="props"
    ></el-cascader>
  </div>
</template>

<script>
import config from "@/config/dict";

export default {
  props: {
    dictType: { type: String, default: "" }, //字典类型
    apiObj: { type: Object, default: () => {} },
    disabledCode: { type: String, default: "" }, //禁用字典选项编码
    checkStrictly: { type: Boolean, default: false }, //是否只可选任意一级
    multiple: { type: Boolean, default: false }, //是否多选
    params: { type: Object, default: () => ({}) },
  },
  data() {
    return {
      dicParams: this.params,
      loading: false,
      options: [],
      props: config.props,
      initloading: false,
    };
  },
  created() {
    this.getRemoteData();
  },
  methods: {
    //获取数据
    async getRemoteData() {
      this.initloading = true;
      var temp = {
        label: this.props.label,
        value: this.props.value,
        children: "childList",
        emitPath: false,
        multiple: this.multiple,
        checkStrictly: this.checkStrictly,
      };
      this.props = temp;
      this.loading = true;
      this.dicParams[config.request.name] = this.dictType;
      var res = {};
      if (this.apiObj) {
        res = await this.apiObj.get(this.params);
      } else if (this.dictType) {
        res = await config.apiObj.post(this.params);
      }
      var response = config.parseData(res);
      var formatData = this.gsObject(response.data);
      this.options = formatData;
      this.loading = false;
      this.initloading = false;
    },
    //格式化数据 添加禁用选项
		gsObject(data) {
			const formatData = [];
			if (data) {
				data.forEach((item) => {
					if (this.disabledCode && (item.dictCode == this.disabledCode || item.parentCodes.indexOf(this.disabledCode) >=0)) {
						item["disabled"] = true;
					}
					this.gsObject(item.childList);
					item["children"] = item.children
						? this.gsObject(item.children)
						: null;
					formatData.push(item);
				});
			}
			return formatData;
		},
  }
};
</script>

<style scoped>
.sc-select {
  display: inline-block;
  position: relative;
}
.sc-select-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  z-index: 100;
  border-radius: 5px;
  border: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  padding-left: 10px;
}
.sc-select-loading i {
  font-size: 14px;
}

[data-theme="dark"] .sc-select-loading {
  background: var(--el-color-white);
  border-color: var(--el-border-color-base);
}
</style>
